﻿using Microsoft.AspNetCore.SignalR;

namespace CIS.Common.SignalR
{
    public class Worker
    {
        public static Worker? Instance;
        private static readonly object _locker = new object();
        private static IHubContext<CISEventHub>? _context;


        public Worker(IHubContext<CISEventHub> context)
        {
            _context = context;
        }

        /// <summary>
        /// 实时数据推送
        /// </summary>
        public static void SendDataToClient(Dictionary<string, object> message)
        {            
             _context?.Clients.All.SendAsync("Data", message);
        }

        /// <summary>
        /// 客户端注册
        /// </summary>
        /// <param name="context"></param>
        public static void Register(IHubContext<CISEventHub> context)
        {
            if (Instance == null)
            {
                lock (_locker)
                {
                    if (Instance == null)
                    {
                        Instance = new Worker(context);
                    }
                }
            }
        }
    }
}
